$(function () {
    getUserInfo();
    $('#btnLogOut').on('click', function () {
        var layer = layui.layer;
        layer.confirm('确认退出登录？', { icon: 3, title: '提示' }, function (index) {
            //do something
            // console.log('ok');
            localStorage.setItem('token', '');
            location.href = '/day_01/login.html';
            layer.close(index);
        });
    })
})
function getUserInfo() {
    $.ajax({
        method: "GET",
        url: "/my/userinfo",
        // headers: {
        //     Authorization: localStorage.getItem('token') || ''
        // },
        success: function (res) {
            // console.log(res);
            if (res.status !== 0) {
                return layui.layer.msg('获取用户信息失败！')
            }
            // 调用 renderAvatar 渲染用户的头像
            renderAvatar(res.data)//res.data
        },


    })
}
function renderAvatar(user) {
    // 1. 获取用户的名称
    var name = user.nickname || user.username;
    // 2. 设置欢迎的文本
    // var first = name[0].toUpperCase();
    $('#welcome').html('欢迎&nbsp;&nbsp;' + name)
    // 3. 按需渲染用户的头像
    if (user.user_pic !== null) {
        $('.layui-nav-img').attr('src', user.user_pic).show();
        $('.text-avatar').hide();
    } else {
        $('.layui-nav-img').hide();
        var first = name[0].toUpperCase();
        $('.text-avatar').html(first).show();
    }
}